package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.CategoriesExample;
import com.campusify.pojo.CategoriesExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.CategoriesMapper;
import com.campusify.pojo.Categories;
import com.campusify.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class CategoriesServiceImpl implements CategoriesService {

	@Autowired
	private CategoriesMapper categoriesMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Categories> findAll() {
		return categoriesMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Categories> page=   (Page<Categories>) categoriesMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Categories categories) {
		categoriesMapper.insert(categories);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Categories categories){
		categoriesMapper.updateByPrimaryKey(categories);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Categories findOne(Integer categoryID){
		return categoriesMapper.selectByPrimaryKey(categoryID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] categoryIDs) {
		for(Integer categoryID:categoryIDs){
			categoriesMapper.deleteByPrimaryKey(categoryID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Categories categories, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		CategoriesExample example=new CategoriesExample();
		Criteria criteria = example.createCriteria();
		
		if(categories!=null){			
						if(categories.getCategoryname()!=null && categories.getCategoryname().length()>0){
				criteria.andCategorynameLike("%"+categories.getCategoryname()+"%");
			}	
		}
		
		Page<Categories> page= (Page<Categories>)categoriesMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
